/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>Project: RabbitMQ - DeadConfig</p>
 * <p>Powered by yzc On 2023-12-23 15:04:06</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Configuration
public class DeadConfig {
    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadQueue(){
        return new Queue("deadQueue");
    }

    /**
     * 死信交换器
     * @return
     */
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange("dead-ex");
    }

    /**
     * 绑定死信队列和交换器
     * @param deadQueue 私信队列名称
     * @param deadExchange 交换器名称
     * @return
     */
    @Bean
    public Binding bindingDead(Queue deadQueue,DirectExchange deadExchange){
        //死信路由键是dead
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("dead");
    }

    /**
     * 正常队列 绑定私信交换器
     * @return
     */
    @Bean
    public Queue normalQueue(){
        return QueueBuilder.durable("normalQueue")
                .withArgument("x-dead-letter-exchange","dead-ex") //指明死信交换器
                .withArgument("x-dead-letter-routing-key","dead") //死信路由键
                .withArgument("x-message-ttl",10000) //队列中的消息ttl
                .build();
    }

    /**
     * 正常交换器
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("normal-ex");
    }

    /**
     * 绑定正常交换器与队列
     */
    @Bean
    public Binding bindingNormal(Queue normalQueue,DirectExchange directExchange){
        return BindingBuilder.bind(normalQueue).to(directExchange).with("normal");
    }
}
